package com.bignerdranch.android.furnitureapplication.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bignerdranch.android.furnitureapplication.ContentActivity;
import com.bignerdranch.android.furnitureapplication.R;
import com.bignerdranch.android.furnitureapplication.entity.RedWoodInfoBean;
import com.bumptech.glide.Glide;

import java.util.List;

public class RedKnowAdapter extends RecyclerView.Adapter<RedKnowAdapter.ViewHolder> {

    private Context mContext;
    private List<RedWoodInfoBean> dataList;

    public class ViewHolder extends RecyclerView.ViewHolder {

        CardView knowItem;
        TextView knowTitle;
        TextView knowContent;
        ImageView knowImage;

        public ViewHolder(View itemView) {
            super(itemView);
            knowItem = itemView.findViewById(R.id.red_know_item);
            knowTitle = itemView.findViewById(R.id.know_title);
            knowContent = itemView.findViewById(R.id.know_content);
            knowImage = itemView.findViewById(R.id.red_wood_image);
        }
    }

    public RedKnowAdapter(List<RedWoodInfoBean> dataList) {
        this.dataList = dataList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mContext == null) {
            mContext = parent.getContext();
        }
        View view = LayoutInflater.from(mContext).inflate(R.layout.red_wood_know_item, parent, false);
        final ViewHolder viewHolder = new ViewHolder(view);
        viewHolder.knowItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = viewHolder.getAdapterPosition();
                RedWoodInfoBean redWoodInfoBean = dataList.get(position);
                Intent intent = new Intent(mContext, ContentActivity.class);
                intent.putExtra("web_url", redWoodInfoBean.getRedwood_url());
                mContext.startActivity(intent);
            }
        });
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        RedWoodInfoBean redWoodInfoBean = dataList.get(position);
        holder.knowTitle.setText(redWoodInfoBean.getRedwood_title());
        holder.knowContent.setText(redWoodInfoBean.getRedwood_content());
        Glide.with(mContext).load(redWoodInfoBean.getRedwood_image()).into(holder.knowImage);
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }
}
